﻿function ProductModel(info) {

    var self = this;

    var id = ko.observable('');
    var title = ko.observable('');
    var thumbUrl = ko.observable('');
    var price = ko.observable(0);
    var shortDescription = ko.observable('');
    var tags = ko.observableArray([]);
    
    function setInfo(data) {
        id(data.prodId);
        title(data.prodName);
        thumbUrl(data.imageUrl);
        price(data.prodPrice);
        shortDescription(data.prodDesc);
        tags(data.tags || []);
    }

    if (info) {
        setInfo(info);
    }

    self.id = id;
    self.title = title;
    self.thumbUrl = thumbUrl;
    self.price = price;
    self.shortDescription = shortDescription;
    self.tags = tags;
}

function FullProductModel(info) {

    var self = this;

    var additional = ko.observableArray([]);
    var id = ko.observable('');
    var title = ko.observable('');
    var avgRating = ko.observable(0);
    var thumbUrl = ko.observable('');
    var price = ko.observable(0);
    var shortDescription = ko.observable('');
    var isbn = ko.observable('');
    var pages = ko.observable('');
    var authors = ko.observableArray([]);
    var brand = ko.observable('');
    var model = ko.observable('');
    var serialNo = ko.observable('');
    var size = ko.observable('');
    var weight = ko.observable('');
    var shipWeight = ko.observable('');
    var tags = ko.observableArray([]);
    var properties = ko.observableArray([]);

    var formattedAuthors = ko.computed(function () {
        var res = '';
        var list = authors();
        if (list) {
            for (var i = 0; i < list.length; i++) {
                var author = list[i];
                if (i != 0) {
                    res += ', ';
                }
                res += author;
            }
        }
        return res;
    });

    function removeTag(tag) {
        tags.remove(tag);
    }

    function setInfo(data) {

        id(data.prodId);
        title(data.prodName);
        shortDescription(data.prodDesc);
        price(data.prodPrice);
        avgRating(data.avgRating);
        thumbUrl(data.imageUrl);
        tags(data.tags || []);
        isbn(data.isbn);
        pages(data.pages);
        authors(data.authors || []);

        brand(data.brand);
        model(data.model);
        serialNo(data.serialNo);
        size(data.size);
        weight(data.weight);
        shipWeight(data.shipWeight);
        additional(dictionaryToArray(data));
    }

    function getInfo() {
        return {
            prodId: id(),
            title: title(),
            imageUrl: thumbUrl(),
            price: price(),
            desc: shortDescription(),
            tags: tags(),
            isbn: isbn(),
            pages: pages(),
            authors: authors(),
            
            brand: brand(),
            model: model(),
            serialNo: serialNo(),
            size: size(),
            weight: weight(),
            shipWeight: shipWeight()
        };
    }
    
    if (info) {
        setInfo(info);
    }

    self.id = id;
    self.title = title;
    self.thumbUrl = thumbUrl;
    self.price = price;
    self.avgRating = avgRating;
    self.shortDescription = shortDescription;
    self.tags = tags;
    self.properties = properties;
    self.isbn = isbn;
    self.pages = pages;
    self.brand = brand;
    self.model = model;
    
    self.additional = additional;
    
    self.serialNo = serialNo;
    self.size = size;
    self.weight = weight;
    self.shipWeight = shipWeight;
    
    self.authors = authors;
    self.formattedAuthors = formattedAuthors;

    self.getInfo = getInfo;
    self.removeTag = removeTag;
}

function dictionaryToArray(dictionary) {
    var result = [];
    for (var key in dictionary) {
        if (dictionary.hasOwnProperty(key)) {
            if (key != 'prodName' && key != 'prodDesc' && key != 'imageUrl' && key != 'tags' && key != 'prodId' && key != 'avgRating' && key != 'prodPrice') {
                result.push({ key: key, value: dictionary[key] });
            }
        }
    }

    return result;
}

function ProductReviewModel(info) {

    var self = this;

    var id = ko.observable('');
    var productId = ko.observable('');
    var userId = ko.observable('');
    var comments = ko.observable('');
    var rating = ko.observable('');
    var reviewDate = ko.observable('');

    function setInfo(data) {
        id(data.reviewId);
        productId(data.prodId);
        userId(data.userId);
        comments(data.comments);
        rating(data.rating);
        reviewDate(data.reviewDate);
    }

    function getInfo() {
        return {
            reviewId: id(),
            prodId: productId(),
            userId: userId(),
            comments: comments(),
            rating: rating(),
            reviewDate: reviewDate()
        };
    }

    if (info) {
        setInfo(info);
    }

    self.id = id;
    self.productId = productId;
    self.userId = userId;
    self.comments = comments;
    self.rating = rating;
    self.reviewDate = reviewDate;
    self.getInfo = getInfo;

}